package org.jeecg.modules.jxc.inventorymanagement.commodityloss.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.jxc.inventorymanagement.commodityloss.entity.JxcDamageLisjxcGoods;
import org.jeecg.modules.jxc.inventorymanagement.commodityloss.entity.JxcDamageList;
import org.jeecg.modules.jxc.inventorymanagement.commodityloss.service.IJxcDamageLisjxcGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: jxc_damage_lisjxc_goods
 * @Author: jeecg-boot
 * @Date: 2021-12-21
 * @Version: V1.0
 */
@Api(tags = "jxc_damage_lisjxc_goods")
@RestController
@RequestMapping("/jxc/jxcDamageLisjxcGoods")
@Slf4j
public class JxcDamageLisjxcGoodsController extends JeecgController<JxcDamageLisjxcGoods, IJxcDamageLisjxcGoodsService> {
    @Autowired
    private IJxcDamageLisjxcGoodsService jxcDamageLisjxcGoodsService;

    /**
     * 分页列表查询
     *
     * @param jxcDamageLisjxcGoods
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "jxc_damage_lisjxc_goods-分页列表查询")
    @ApiOperation(value = "jxc_damage_lisjxc_goods-分页列表查询", notes = "jxc_damage_lisjxc_goods-分页列表查询")
    @GetMapping(value = "/list")
    public ResponseEntity<?> queryPageList(JxcDamageLisjxcGoods jxcDamageLisjxcGoods,
                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                  HttpServletRequest req) {
        IPage<JxcDamageLisjxcGoods> pageList = null;
        try {
            QueryWrapper<JxcDamageLisjxcGoods> queryWrapper = QueryGenerator.initQueryWrapper(jxcDamageLisjxcGoods, req.getParameterMap());
            Page<JxcDamageLisjxcGoods> page = new Page<JxcDamageLisjxcGoods>(pageNo, pageSize);
            pageList = jxcDamageLisjxcGoodsService.page(page, queryWrapper);
        }catch (Exception e){
            return ResponseEntity.status(500).build();
        }
        return ResponseEntity.ok(pageList);
    }

    /**
    * 报损单添加
    * @description:
    * @param damageNumber
* @param damageListGoodsStr
* @param jxcDamageLisjxcGoodsList
* @param request
    * @return org.springframework.http.ResponseEntity<?>
    * @exception
    */
    @AutoLog(value = "jxc_damage_lisjxc_goods-添加")
    @ApiOperation(value = "jxc_damage_lisjxc_goods-添加", notes = "jxc_damage_lisjxc_goods-添加")
    @PostMapping(value = "/save")
    public ResponseEntity<?> add(@RequestParam String damageNumber
            ,@RequestParam String damageListGoodsStr
            ,@RequestParam String jxcDamageLisjxcGoodsList
            ,HttpServletRequest request) {
        JxcDamageLisjxcGoods[] damageLisjxcGoodsList = JSON.parseObject(jxcDamageLisjxcGoodsList, JxcDamageLisjxcGoods[].class);
        try {
            /*HttpSession session = request.getSession();
            Integer userId = (Integer)session.getAttribute("userId");*/
            Integer userId = 1;
                    jxcDamageLisjxcGoodsService.saveDamage(damageNumber,damageListGoodsStr,damageLisjxcGoodsList,userId);
        }catch (Exception e){
            return ResponseEntity.status(500).build();
        }
        return ResponseEntity.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param jxcDamageLisjxcGoods
     * @return
     */
    @AutoLog(value = "jxc_damage_lisjxc_goods-编辑")
    @ApiOperation(value = "jxc_damage_lisjxc_goods-编辑", notes = "jxc_damage_lisjxc_goods-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody JxcDamageLisjxcGoods jxcDamageLisjxcGoods) {
        jxcDamageLisjxcGoodsService.updateById(jxcDamageLisjxcGoods);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "jxc_damage_lisjxc_goods-通过id删除")
    @ApiOperation(value = "jxc_damage_lisjxc_goods-通过id删除", notes = "jxc_damage_lisjxc_goods-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        jxcDamageLisjxcGoodsService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "jxc_damage_lisjxc_goods-批量删除")
    @ApiOperation(value = "jxc_damage_lisjxc_goods-批量删除", notes = "jxc_damage_lisjxc_goods-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.jxcDamageLisjxcGoodsService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "jxc_damage_lisjxc_goods-通过id查询")
    @ApiOperation(value = "jxc_damage_lisjxc_goods-通过id查询", notes = "jxc_damage_lisjxc_goods-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        JxcDamageLisjxcGoods jxcDamageLisjxcGoods = jxcDamageLisjxcGoodsService.getById(id);
        if (jxcDamageLisjxcGoods == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(jxcDamageLisjxcGoods);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param jxcDamageLisjxcGoods
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, JxcDamageLisjxcGoods jxcDamageLisjxcGoods) {
        return super.exportXls(request, jxcDamageLisjxcGoods, JxcDamageLisjxcGoods.class, "jxc_damage_lisjxc_goods");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, JxcDamageLisjxcGoods.class);
    }

    /***
    * @description: 报损单查询
    * @param sTime
    * @return
    * @exception
    */
    @PostMapping("/listByTime")
    public Map<String,Object> dList(String sTime){
        Map<String, Object> map = new HashMap<>();
        List<Map<String,Object>> list = jxcDamageLisjxcGoodsService.queryList(sTime);
        map.put("rows",list);
        return map;
    }
    /***
    * @description: 查询报损商品详情
    * @param damageListId
    * @return java.util.Map<java.lang.String,java.lang.Object>
    * @exception
    */
    @PostMapping("/goodsList")
    public Map<String,Object> gList(Integer damageListId){
        Map<String, Object> map = new HashMap<>();
        List<Map<String,Object>> list = jxcDamageLisjxcGoodsService.queryGoodsList(damageListId);
        map.put("rows",list);
        return map;
    }
}
